﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Data;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class BillReturnVisitSetting : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
            }
        }
        private void BindList(int pageindex)
        {
            string sqlWhere = " and PaperInvoicesNo is not null ";
            if (MaintainDateFrom.SelectedDate != null)
            {
                sqlWhere += " and CreatedOn >='"+ MaintainDateFrom.SelectedDate.ToString()+"'";
            }
            if (MaintainDateTo.SelectedDate != null)
            {
                sqlWhere += " and CreatedOn <'" + MaintainDateTo.SelectedDate.ToString() + "'";
            }
            sqlWhere += " and maintainID not in (select MaintainID from Cms_Wsm_MaintainEvaluation)";
          
            string sql = "";
            if (chkRandom.Checked)
            {
                if (VisitCount.Value == null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('请输入抽取数量');", true);
                    return;
                }
                Int32 count = System.Convert.ToInt32(VisitCount.Value);
                if (count > 30)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('抽取数量不能大于30');", true);
                    return;
                }
                sql = @" select top " + count + " * from Cms_Wsm_MaintainBill where 1=1 and UsersContacter in(select contactorID from Cms_Wsm_CustomerContactor where ContactorPhone is not null)";
//                sql += @"(select RegisterID from Cms_Wsm_TaskRegister where customerid  in
//                    (
//                    select customerid from dbo.Cms_Wsm_CustomerContactor where 
//                    ))";
                sql = sql + sqlWhere+" order by NEWID()";
            }
            else
            {
                if (hiddenUserID.Value != "") {
                    sqlWhere += " and maintainID  in ( select MaintainID from Cms_Wsm_MaintainEvaluation where RegisterID in ( select RegisterID from Cms_Wsm_TaskRegister where CustomerID = '" + hiddenUserID.Value + "'))";
                }
                if (employeeGUID.Value != "") {
                    sqlWhere += " and Maintainer = '" + employeeGUID.Value + "'";
                }
                sql = @"select * from Cms_Wsm_MaintainBill where UsersContacter in(select contactorID from Cms_Wsm_CustomerContactor where ContactorPhone is not null)";
//                sql += @"(select RegisterID from Cms_Wsm_TaskRegister where customerid  in
//                    (
//                    select customerid from dbo.Cms_Wsm_CustomerContactor
//                    )) ";
                sql = sql + sqlWhere;
            }
            DataTable list = new CommonBll().getDataFromSQL(sql);
            RepeaterEvaluation.DataSource = list;
            RepeaterEvaluation.DataBind();
            }
       
        protected void RepeaterEvaluation_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
        }    

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterEvaluation_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label maintainPaperNum = e.Item.FindControl("maintainPaperNum") as Label;
            Label MaintainID = e.Item.FindControl("MaintainID") as Label;
            Label MaintainerName = e.Item.FindControl("MaintainerName") as Label;
            HyperLink hlcustomer = e.Item.FindControl("hlcustomer") as HyperLink;
            Label InstrumentName = e.Item.FindControl("InstrumentName") as Label;
            Label CreatedBy = e.Item.FindControl("CreatedBy") as Label;
            Label CreatedByName = e.Item.FindControl("CreatedByName") as Label;
            if (MaintainID != null)
            {
                WsmMaintainBillEntity maintainEntity = new SystemBll().Get<WsmMaintainBillEntity>(new Guid(MaintainID.Text));
                WsmTaskRegisterEntity registerEntity = new SystemBll().Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);              
                if (maintainEntity.Maintainer != Guid.Empty)
                {
                    MaintainerName.Text = new SystemBll().Get<SysUserEntity>(maintainEntity.Maintainer).UserName;
                }
                hlcustomer.Text = new SystemBll().Get<CtrtCustomerEntity>(registerEntity.CustomerID).CustomerName;
                hlcustomer.NavigateUrl = "../SyncManage/CustomerInfo.aspx?CustomerID=" + registerEntity.CustomerID.ToString();
                if (registerEntity.InstrumentID.ToString() != "00000000-0000-0000-0000-000000000000")
                {
                    InstrumentName.Text = new SystemBll().Get<WsmInstrumentInfoEntity>(registerEntity.InstrumentID).instrumentName;
                }
                CreatedByName.Text = new SystemBll().Get<SysUserEntity>(new Guid(CreatedBy.Text)).UserName;                
            }
        } 
    }
}
